import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'


Vue.use(ElementUI);

Vue.prototype.$axios = axios
axios.defaults.baseURL = 'http://127.0.0.1:3000'
// axios.defaults.baseURL = 'http://liangwei.tech:3000'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  if (window.localStorage.getItem('token') && !config.headers.Authorization) {
    config.headers.Authorization = 'Bearer ' + window.localStorage.getItem('token')
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

Vue.config.productionTip = false

// 路由管理
router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    if (window.localStorage.getItem('token')) {
      next()
    } else {
      router.push('/login')
    }
  } else {
    next()
  }
})

new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
